<template>
  <div class="standard-table">
    <div v-if="$slots.header" class="standard-table-header">
      <slot name="header"></slot>
    </div>
    <el-table
      ref="table"
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      size="medium"
      :border="border"
      :max-height="maxHeight"
      :min-height="minHeight"
      :row-key="rowKey"
      :show-summary="showSummary"
      :data="datasource"
      :show-header="showHeader"
      :highlight-current-row="highlightCurrentRow"
      :row-style="rowStyle"
      :span-method="spanMethod"
      :expand-row-keys="expandRowKeys"
      :row-class-name="rowClassName"
      :summary-method="summaryMethod"
      @select="handleSelect"
      @selection-change="handleSelectionChange"
      @current-change="handleCurrentChange"
      @expand-change="handleExpandChange"
      @row-click="handleRowClick"
      @row-dblclick="handleRowDblclick"
      @cell-mouse-enter="handleCellMouseEenter"
      @cell-mouse-leave="handleCellMouseLeave"
      @sort-change="handSortChange"
    >
      <el-table-column
        v-if="type === 'selection'"
        width="44"
        type="selection"
        :reserve-selection="reserveSelection"
        :selectable="selectable"
      ></el-table-column>
      <template v-for="(column, index) in columns">
        <el-table-column
          v-if="!column.hidden"
          :key="index"
          :label="column.label"
          :prop="column.prop"
          :width="column.width"
          :fixed="column.fixed"
          :type="column.type"
          :sortable="column.sortable"
          :min-width="column.minWidth"
          :render-header="column.renderHeader"
        >
          <template slot-scope="scope">
            <span v-if="!column.render">{{ scope.row[column.prop] }}</span>
            <render-column
              v-else
              :render="column.render"
              :row="scope.row"
              :index="index"
              :column="column"
              :data="datasource"
            ></render-column>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <el-pagination
      v-if="pagination && paginationNum"
      background=""
      layout="total, prev, pager, next,sizes, jumper"
      :total="pagination.total"
      :page-size="pagination.pageSize"
      :page-sizes="paginationNum"
      :current-page="pagination.currentPage"
      @size-change="handleSizeChange"
      @current-change="handleTableChange"
    ></el-pagination>
    <el-pagination
      v-else
      background=""
      layout="total, prev, pager, next,sizes, jumper"
      :total="pagination.total"
      :page-size="pagination.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :current-page="pagination.currentPage"
      @size-change="handleSizeChange"
      @current-change="handleTableChange"
    ></el-pagination>
  </div>
</template>

<script>
import RenderColumn from "./render-column";

export default {
  name: "StandardTable",
  components: {
    RenderColumn,
  },
  props: {
    type: {
      type: String,
    },
    border: {
      type: Boolean,
      default: false,
    },
    showSummary: {
      type: Boolean,
      default: false,
    },
    rowKey: {},
    reserveSelection: {
      type: Boolean,
      default: false,
    },
    loading: {
      type: Boolean,
      default: true,
    },
    columns: {
      type: Array,
      default() {
        return [];
      },
    },
    datasource: {
      type: Array,
      default() {
        return [];
      },
    },
    pagination: {
      type: Object,
    },
    paginationNum: {
      type: Array,
    },
    maxHeight: {
      type: [String, Number],
    },
    minHeight: {
      type: [String, Number],
    },
    minWidth: {
      type: [String, Number],
    },
    highlightCurrentRow: {
      type: Boolean,
    },
    expandRowKeys: {
      type: Array,
    },
    showHeader: {
      type: Boolean,
      default: true,
    },
    rowStyle: {
      type: Function,
    },
    rowClassName: {
      type: Function,
    },
    summaryMethod: {
      type: Function,
    },
    spanMethod: {
      type: Function,
    },
    selectable: {
      type: Function,
    },
    onChange: {
      type: Function,
    },
    onChangeSize: {
      type: Function,
    },
    onSelect: {
      type: Function,
    },
    onSelectionChange: {
      type: Function,
    },
    onCurrentChange: {
      type: Function,
    },
    onExpandChange: {
      type: Function,
    },
    onRowClick: {
      type: Function,
    },
    onSortChange: {
      type: Function,
    },
    onRowDblclick: {
      type: Function,
    },
    onCellMouseEenter: {
      type: Function,
    },
    onCellMouseLeave: {
      type: Function,
    },
    tableIndex: {
      type: [Number, String],
    },
  },
  methods: {
    handleSizeChange(pageNum) {
      const { onChangeSize } = this;

      if (typeof onChangeSize === "function") onChangeSize(pageNum);
    },
    handleTableChange(currentPage) {
      const { onChange, pagination, tableIndex } = this;

      pagination.currentPage = currentPage;

      if (typeof onChange === "function") onChange(currentPage, tableIndex);
    },
    handleSelect(selection, row) {
      const { onSelect } = this;

      if (typeof onSelect === "function") onSelect(selection, row);
    },
    handleSelectionChange(selection) {
      const { onSelectionChange } = this;

      if (typeof onSelectionChange === "function") onSelectionChange(selection);
    },
    handleCurrentChange(row) {
      const { onCurrentChange } = this;
      if (typeof onCurrentChange === "function") onCurrentChange(row);
    },
    handleExpandChange(row, expandedRows) {
      const { onExpandChange } = this;
      if (typeof onExpandChange === "function")
        onExpandChange(row, expandedRows);
    },
    handleRowClick(row, event, column) {
      const { onRowClick } = this;
      if (typeof onRowClick === "function") onRowClick(row, event, column);
    },
    handSortChange(row) {
      const { onSortChange } = this;
      if (typeof onSortChange === "function") onSortChange(row);
    },
    handleRowDblclick(row, event, column) {
      const { onRowDblclick } = this;
      if (typeof onRowDblclick === "function")
        onRowDblclick(row, event, column);
    },
    handleCellMouseEenter(row, column, cell, event) {
      const { onCellMouseEenter } = this;
      if (typeof onCellMouseEenter === "function")
        onCellMouseEenter(row, column, cell, event);
    },
    handleCellMouseLeave(row, column, cell, event) {
      const { onCellMouseLeave } = this;
      if (typeof onCellMouseLeave === "function")
        onCellMouseLeave(row, column, cell, event);
    },

    toggleRowSelection(row, selected = true) {
      this.$refs.table.toggleRowSelection(row, selected);
    },
    toggleRowExpansion(row, expanded = true) {
      this.$refs.table.toggleRowExpansion(row, expanded);
    },
    clearSelection() {
      this.$refs.table.clearSelection();
    },
    setCurrentRow(row) {
      this.$refs.table.setCurrentRow(row);
    },
  },
};
</script>

<style>
.standard-table-header {
  padding: 8px 10px;
  border: solid #ebeef5;
  border-width: 1px 1px 0 1px;
}
.standard-table .el-table {
  border: solid #ebeef5;
  border-width: 1px 1px 0 1px;
  text-align: center;
}
.standard-table .el-table__header th {
  padding: 6px 0;
  background-color: #fff;
  text-align: center;
  height: 44px;
  color: #000;
  font-weight: bold;
}
.standard-table .el-table tbody .el-table__row td {
  text-align: center;
}
.standard-table .el-table__row .cell-actions a {
  display: inline-block;
  padding-right: 6px;
  line-height: 1;
  text-decoration: none;
  color: #1890ff;
}
.standard-table .el-table__row .cell-actions a:hover {
  text-decoration: underline;
}
.standard-table .el-pagination {
  padding-top: 10px;
  padding-bottom: 10px;
  text-align: right;
}
</style>

